#-*-coding:utf-8-*-
__author__ = 'toriycn'

import urllib3
from html.parser import HTMLParser


class stack:
    def __init__(self,size=100,list=None):
        self.contain=[]
        self.msize=size;
        self.top = 0;
    def getTop(self):
        if(self.top>0):
            return self.contain[self.top-1]
        else:
            return None
    def getLength(self):
        return len(self.contain);
    def push(self,data):
        if(self.top==self.msize):
            return -1
        self.contain.append(data)
        self.top=self.top +1
    def pop(self):
        try:
            res=self.contain.pop()
            if(self.top>0):
                self.top=self.top-1
            return res;
        except IndexError:
            return None


class ParserOschinaNew(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.st=stack(size=1000)
        self.st.push('over')

    def handle_starttag(self,tag,attrs):
        stack_size = self.st.getLength()
        if stack_size==1 and tag=='tr':
           self.st.push('tr')
        if (stack_size==2 and tag =='th' ):
            for name,value in attrs:
                if (name=='class' and value=='txtd fz14'):
                    self.st.push('th')
                    text = '标题：'
                    print ('%s'%text)
        if (stack_size==3 and tag=='a' ):
            for name,value in attrs:
                if (name=='target' and value=='_blank'):
                    self.st.push('a')


                    
                    text = '内容：'
                    print ('%s'%text)

    def handle_data(self ,data):
        stack_size = self.st.getLength()
        if stack_size==4:
           print(data)

    def handle_endtag(self,tag):
        stack_size = self.st.getLength()
        stack_tag = self.st.getTop()
        if ('a'==tag and 'a'==stack_tag):
            self.st.pop()
        if ('th'==tag and 'th'==stack_tag):
            self.st.pop()
        if ('tr'==tag and 'tr'==stack_tag):
            self.st.pop()
            if('over'==self.st.getTop()):
                print ("this is end!")




a = []
a.append('a')

a.append('b')

a.append('c')
print ('a' in a )

http = urllib3.PoolManager()

headers = urllib3.util.request.make_headers(keep_alive=True, user_agent="'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'")
r = http.request('GET', 'http://bbs.dospy.com/forum-141-1.html',None,headers)

#print(r.data.decode('UTF_8','ignore'))

print(r.data.decode('GBK','ignore'))

hp = ParserOschinaNew()
hp.feed(r.data.decode('GBK','ignore'))
hp.close()
print(hp.st.contain)
